package nc.gk.gkxm.service.impl;

import io.jsonwebtoken.Claims;
import nc.gk.gkxm.mapper.GenerateQuesttionMapper;
import nc.gk.gkxm.mapper.UserMapper;
import nc.gk.gkxm.model.entity.User;
import nc.gk.gkxm.model.other.JwtProperties;
import nc.gk.gkxm.service.GenerateQuesttionService;
import nc.gk.gkxm.utils.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

@Service
public class GenerateQuesttionServiceImpl implements GenerateQuesttionService {
    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private UserMapper userMapper;
    @Resource
    private GenerateQuesttionMapper generateQuesttionMapper;
    @Override
    @Transactional
    public String getQuestionInfo(String token, Map<String, Object> map) {
//        解析token
        Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);
        Long userId = Long.valueOf(claims.get("userId").toString());
//        获取用户信息
        User user=userMapper.getUserByCode(String.valueOf(userId));
        if(user==null){
            throw new RuntimeException("用户不存在");
        }
        //获取所选中的分类信息
        ArrayList<Integer> sortIds = (ArrayList<Integer> ) map.get("sortIds");
        if(sortIds==null||sortIds.size()==0){
            throw new RuntimeException("请选择分类");
        }
        //传入参数
        Integer a=generateQuesttionMapper.generateQuestion(user.getCode(),sortIds);
        if(a.intValue()==0||a.intValue()==-1){
            return "自动生成成功，但是所选类型不存在对应的题目！请前往个人维护中选择其他类型的题目或联系管理维护该类型题库";
        }
        return "生成成功";
    }
}
